package com.ynkbny.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynkbny.domain.PlanData;
import com.ynkbny.domain.dto.PlanDataDTO;
import com.ynkbny.mapper.PlanDataMapper;
import com.ynkbny.service.PlanDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 准备数据-表服务实现类
 *
 * @author : juzipi123
 * @date : 2023-8-1
 */
@Slf4j
@Service
public class PlanDataServiceImpl extends ServiceImpl<PlanDataMapper, PlanData> implements PlanDataService {

    @Resource
    private PlanDataMapper mapper;

    /**
     * 新增
     *
     * @author : juzipi123
     * @param planData 参数
     * @return
     */
    @Override
    public String insert(PlanData planData) {
        //查询，没有数据才插入，有数据就更新
        String enterpriseCode = planData.getEnterpriseCode();
        String deviceId = planData.getDeviceId();
        String start = planData.getStart();
        String end =planData.getEnd();
        PlanDataDTO planData1 = mapper.selectRepeat(enterpriseCode,deviceId, start,end );
        if(ObjectUtil.isEmpty(planData1)){
            // 获取当前时间
            Date currentDate = new Date();
            // 定义日期格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 将当前时间转换为指定格式的字符串
            String dateString = dateFormat.format(currentDate);
            planData.setStatDate(dateString);
            this.save(planData);
            return "新增成功";
        }
        PlanDataDTO planDataDTO = new PlanDataDTO();
        BeanUtils.copyProperties(planData,planDataDTO);
        planDataDTO.setId(planData1.getId());
        mapper.updateById2(planDataDTO);
        return "修改成功";
    }
}